###########################################
#  Primary Divisions: How Voters Evaluate Policy and Group Differences in Intra-Party Contests
#   - Forthcoming at The Journal of Politics
#   - Henderson et al 2021
#
###########################################
#  - code by S. Goggin & J. Henderson
########################################################
# This file produces descriptives/statistics assessing mechanisms for heterogeneity in agreement x consistency in Table 1
########################################################

#dirs="~/Dropbox/replication0/"
#dirs should be set here or in runR.R

rm(list=ls()[which(ls()!='dirs')])
library(ggplot2)
library(stringr)

# messy function to reorder by some estimate value
lableOrder=function(xmat,labels,label.groups,omits,o.column){

	# denote which label is to be omitted on the label
	for(i in 1:length(omits)){
		labels[which(labels==omits[i])]=paste('omit',labels[which(labels==omits[i])],sep='_')
	}

	# break groups into levels
	un_group=unique(label.groups)

	# this is the item to sort on, typically global or independent
	xm=xmat[,o.column]

	# vector which will contain row order
	xo=1:length(xm)

	# rearranging roworder within level
	for(j in 1:length(un_group)){
		ix=which(label.groups==un_group[j])
		if(length(ix)>2){
			ix=ix[!grepl(labels[ix],pattern='omit')]
			xo[ix]=xo[ix][order(xm[ix])]
		}
	}
	return(xmat[xo,])
}

reOrder=function(x,o){
	ix=array(NA,nrow(x))
	for(i in 1:length(o)){
		ix[i]=which(x$iv_order==o[i])
	}
	return(x[ix,])
}

setwd(dirs)
load("data/cces_stacked_unmatched.Rdata")


###########################################
###First, need to stack based on candidates, not just candidate pairs (and also get text out for labels later)

#This has leaners as independents, which is incorrect
#cces_stacked$pid3clean <- ifelse(cces_stacked$pid3=="Democrat",-1,ifelse(cces_stacked$pid3=="Republican",1,0))

library(car)

load("data/data_matrix_scored.Rdata")
candidate_matrix=data_matrix
#candidate_matrix=read.csv("csv/data_matrix_scored.csv",header=T,stringsAsFactors=F)[,-c(1)]

###########################################
###Then, produce models w/ Clustered SEs

#Function from: http://scholar.byu.edu/jgubler/book/clustered-standard-errors-r
#Need this for clustered standard errors
clse.f <- function(dat,fm, cluster){
 require(sandwich)
 require(lmtest)
 not <- attr(fm$model,"na.action")
if( ! is.null(not)){
  cluster <- cluster[-not]
    dat <- dat[-not,]
}
 with(dat,{
 M <- length(unique(cluster))
 N <- length(cluster)
 K <- fm$rank
 dfc <- (M/(M-1))*((N-1)/(N-K))
 uj <- apply(estfun(fm),2, function(x) tapply(x, cluster, sum));
 vcovCL <- dfc*sandwich(fm, meat=crossprod(uj)/N)
 coeftest(fm, vcovCL)
 }
 )
}

############################################################
############################################################
### Primary Elections (by PID)

####

#candidate_matrix$tr=as.numeric(candidate_matrix$cq==1)
#candidate_matrix$tr[which(candidate_matrix$cq==4)]=NA

#candidate_matrix$ii_scores = candidate_matrix$scores*candidate_matrix$self_place

# include factional endorsement affinity == ff is non-policy identity affinity for groups
# include factional endorsement affinity == ee is policy identity affinity for groups
candidate_matrix$older_white = as.numeric(candidate_matrix$age>=45 & candidate_matrix$white==1)
kk=which(names(candidate_matrix)=='religion')[2]
candidate_matrix$christian = as.numeric(candidate_matrix[,kk]=='Protestant' | candidate_matrix[,kk]=='Roman Catholic' | candidate_matrix[,kk]=='Mormon')
candidate_matrix$militaryhh = as.numeric(candidate_matrix$"milstat_1"=='Yes' | candidate_matrix$"milstat_2"=='Yes' | candidate_matrix$"milstat_3"=='Yes' ) # might be too broad since it includes family
candidate_matrix$female_single = as.numeric(candidate_matrix$female==1 & candidate_matrix$married == 0)
candidate_matrix$black  = candidate_matrix$black
candidate_matrix$hisps = candidate_matrix$hisps
candidate_matrix$unionhh = candidate_matrix$unionhh
candidate_matrix$investor = candidate_matrix$investor


candidate_matrix$ff_reproductive = as.numeric(candidate_matrix$e_reproductive == 1 & candidate_matrix$female_single == 1)
candidate_matrix$ff_civilrights  = as.numeric(candidate_matrix$e_civilrights  == 1 & (candidate_matrix$black == 1 | candidate_matrix$hisps == 1))
candidate_matrix$ff_laborunions  = as.numeric(candidate_matrix$e_laborunions  == 1 & candidate_matrix$unionhh == 1)
candidate_matrix$ff_veterans     = as.numeric(candidate_matrix$e_veterans     == 1 & candidate_matrix$militaryhh == 1)
candidate_matrix$ff_business     = as.numeric(candidate_matrix$e_business     == 1 & candidate_matrix$investor == 1)
candidate_matrix$ff_taxreform    = as.numeric(candidate_matrix$e_taxreform    == 1 & candidate_matrix$investor == 1)
candidate_matrix$ff_christian    = as.numeric(candidate_matrix$e_christian    == 1 & candidate_matrix$christian == 1)
candidate_matrix$ff_teaparty     = as.numeric(candidate_matrix$e_teaparty     == 1 & candidate_matrix$older_white == 1)

candidate_matrix$ff_energy       = as.numeric(candidate_matrix$e_energy      == 1 & candidate_matrix$mip_env == 1)
candidate_matrix$ff_environment  = as.numeric(candidate_matrix$e_environment == 1 & candidate_matrix$mip_env  == 1)
candidate_matrix$ff_guncontrol   = as.numeric(candidate_matrix$e_guncontrol  == 1 & candidate_matrix$mip_guns == 1)
candidate_matrix$ff_gunrights    = as.numeric(candidate_matrix$e_gunrights   == 1 & candidate_matrix$mip_guns == 1)

#vote <- subset(candidate_matrix,candidate_matrix$conjoints==3|candidate_matrix$conjoints==8)
#vote$pty <- ifelse(vote$conjoints==3,0,1)
#vote=candidate_matrix
un_id=unique(candidate_matrix$respondent)
vote=as.data.frame(matrix(NA,length(un_id),ncol(candidate_matrix)))
for(i in 1:length(un_id)){
	ix=which(un_id[i]==candidate_matrix$respondent)[1]
	vote[i,]=candidate_matrix[ix,]
}
names(vote)=names(candidate_matrix)


################################################
################################################
# descriptives: characterize on which issues cross pressured are crosspressured as k increases
################################################
################################################

#add up k=crossP score
libPos=
as.numeric(vote$libcon_envs<0)+as.numeric(vote$libcon_guns<0)+
as.numeric(vote$libcon_taxs<0)+as.numeric(vote$libcon_gays<0)+
as.numeric(vote$libcon_trad<0)+
as.numeric(vote$libcon_need<0)+
as.numeric(vote$libcon_immi<0)+as.numeric(vote$libcon_abrt<0)+
as.numeric(vote$libcon_dfns<0)+
as.numeric(vote$libcon_crme<0)

libMat=cbind(
	as.numeric(vote$libcon_envs<0),as.numeric(vote$libcon_guns<0),
	as.numeric(vote$libcon_taxs<0),as.numeric(vote$libcon_gays<0),
	as.numeric(vote$libcon_trad<0),
	as.numeric(vote$libcon_need<0),
	as.numeric(vote$libcon_immi<0),as.numeric(vote$libcon_abrt<0),
	as.numeric(vote$libcon_dfns<0),
	as.numeric(vote$libcon_crme<0)
)
conPos=
as.numeric(vote$libcon_envs>0)+as.numeric(vote$libcon_guns>0)+
as.numeric(vote$libcon_taxs>0)+as.numeric(vote$libcon_gays>0)+
as.numeric(vote$libcon_trad>0)+
as.numeric(vote$libcon_need>0)+
as.numeric(vote$libcon_immi>0)+as.numeric(vote$libcon_abrt>0)+
as.numeric(vote$libcon_dfns>0)+
as.numeric(vote$libcon_crme>0)

conMat=cbind(
	as.numeric(vote$libcon_envs>0),as.numeric(vote$libcon_guns>0),
	as.numeric(vote$libcon_taxs>0),as.numeric(vote$libcon_gays>0),
	as.numeric(vote$libcon_trad>0),
	as.numeric(vote$libcon_need>0),
	as.numeric(vote$libcon_immi>0),as.numeric(vote$libcon_abrt>0),
	as.numeric(vote$libcon_dfns>0),
	as.numeric(vote$libcon_crme>0)
)
colnames(conMat)=colnames(libMat)=c(
'libcon_envs','libcon_guns',
'libcon_taxs','libcon_gays',
'libcon_trad',
'libcon_need',
'libcon_immi','libcon_abrt',
'libcon_dfns',
'libcon_crme')

libPos=rowSums(libMat)
conPos=rowSums(conMat)

table(conPos[which(vote$pid==-1)])/sum(table(conPos[which(vote$pid==-1)]))
table(libPos[which(vote$pid==1)])/sum(table(libPos[which(vote$pid==1)]))
pid=vote$pid

#> table(conPos[which(vote$pid==-1)])
#           0            1            2            3            4            5            6            7            8            9
#0.1642808453 0.3888888889 0.1734832993 0.1550783913 0.0657805044 0.0361281527 0.0105657805 0.0040899796 0.0010224949 0.0006816633
#> table(libPos[which(vote$pid==1)])/sum(table(libPos[which(vote$pid==1)]))
#           0            1            2            3            4            5            6            7            8            9           10
#0.1219971738 0.1667451719 0.1865284974 0.1521431936 0.1403674046 0.1031559114 0.0753650495 0.0372114932 0.0131888837 0.0023551578 0.0009420631
do.x=F
if(do.x==T){
xm=rbind(
#(colMeans(conMat[which(conPos==0 & pid==-1),]))/1,
(colMeans(conMat[which(conPos==1 & pid==-1),]))/1,
(colMeans(conMat[which(conPos==2 & pid==-1),]))/1,
(colMeans(conMat[which(conPos==3 & pid==-1),]))/1,
(colMeans(conMat[which(conPos==4 & pid==-1),]))/1,
(colMeans(conMat[which(conPos==5 & pid==-1),]))/1,
(colMeans(conMat[which(conPos==6 & pid==-1),]))/1,
(colMeans(conMat[which(conPos==7 & pid==-1),]))/1,
(colMeans(conMat[which(conPos==8 & pid==-1),]))/1)
#(colMeans(conMat[which(conPos==9 & pid==-1),]))/1)
xm=round(xm,digits=4)
xm=xm[1:6,order(colMeans(xm))]

palette <- colorRampPalette(colors=c( 'white','firebrick2'))
cols <- palette(800)
#pdf(file='~/Dropbox/Guess_My_Party/IssueTable/CMAG/issues_heatmap_cnt.pdf')
heatmap((xm), col=cols,Rowv=NA,Colv=NA,margins=c(8,8),symm = F)
#dev.off()
# idea is that correlation between issues

xm=rbind(
#(colMeans(libMat[which(libPos==0 & pid==-1),]))/1,
(colMeans(libMat[which(libPos==1 & pid==1),]))/1,
(colMeans(libMat[which(libPos==2 & pid==1),]))/1,
(colMeans(libMat[which(libPos==3 & pid==1),]))/1,
(colMeans(libMat[which(libPos==4 & pid==1),]))/1,
(colMeans(libMat[which(libPos==5 & pid==1),]))/1,
(colMeans(libMat[which(libPos==6 & pid==1),]))/1,
(colMeans(libMat[which(libPos==7 & pid==1),]))/1,
(colMeans(libMat[which(libPos==8 & pid==1),]))/1)
#(colMeans(libMat[which(libPos==9 & pid==-1),]))/1)
xm=round(xm,digits=4)
xm=xm[1:6,order(colMeans(xm))]

#diag(countAds)=NA
palette <- colorRampPalette(colors=c( 'white','dodgerblue3'))
cols <- palette(800)
#pdf(file='~/Dropbox/Guess_My_Party/IssueTable/CMAG/issues_heatmap_cnt.pdf')
heatmap((xm), col=cols,Rowv=NA,Colv=NA,margins=c(8,8),symm = F)
#dev.off()
}
# what does a random matrix look like?
#randomConMat=matrix(0,nrow(conMat),ncol(conMat))
#randomLibMat=matrix(0,nrow(libMat),ncol(libMat))
#for(j in 1:ncol(conMat)){#
	#randomConMat[,j]=conMat[sample(1:nrow(conMat),replace=F),j]
	#sample(c(0,1),prob=table(conMat[,j])/sum(table(conMat[,j])),size=nrow(conMat),replace=T)
	#randomLibMat[,j]=
	#sample(c(0,1),prob=table(libMat[,j])/sum(table(libMat[,j])),size=nrow(libMat),replace=T)
#}


# by type approach
lib_type=array(NA,nrow(libMat))
con_type=array(NA,nrow(conMat))
for(j in 1:nrow(conMat)){
	con_type[j]=paste(conMat[j,],collapse='_')
	lib_type[j]=paste(libMat[j,],collapse='_')
}


#for(j in 1:max(conPos[which(pid==-1)])){
#	plot(sort(table(con_type[which(conPos==j & pid==-1)])),ylim=c(0,120),ylab='')
#}

#for(j in 1:max(libPos[which(pid==1)])){
#	plot(sort(table(lib_type[which(libPos==j & pid==1)])),ylim=c(0,120),ylab='')
#}
# idea/arg:: measurement point that increasing cross-pressure is less and less systematic in influence


###### signal in the noise
# a) number of peolple of my type
pdf("appendix/figures/mixed_profile_types.pdf", width=8, height=5, pointsize=1)
	plot(table(c(con_type[pid==-1],lib_type[pid==1])),axes=F,ylab='Frequency of Type',xlab='Attitude Profile #')
	axis(1,);axis(2,)
	text(x=175,y=600,label='2 unique ways to be (mostly) policy consistent',cex=2,col='darkblue')
	text(x=275,y=200,label=paste(length(table(c(con_type[pid==-1],lib_type[pid==1])))-2,' unique ways to be policy inconsistent',sep=''),cex=2,col='red')
dev.off()

# b) number of people who agree with my policies, including other types
# every individual gets a score
peerAgreementCon=matrix(0,nrow(conMat),2)
peerAgreementLib=matrix(0,nrow(libMat),2)
for(i in 1:nrow(conMat)){
	i1=which(conMat[i,]==1)
	i0=which(conMat[i,]==0)
	if(length(i0)>0){
		peerAgreementCon[i,1]=length(which(rowSums(abs(cbind(0,conMat[i,i0]-conMat[-c(i),i0])))==0))
	}
	if(length(i1)>0){
		peerAgreementCon[i,2]=length(which(rowSums(abs(cbind(0,conMat[i,i1]-conMat[-c(i),i1])))==0))
	}
	i1=which(libMat[i,]==1)
	i0=which(libMat[i,]==0)
	if(length(i0)>0){
		peerAgreementLib[i,1]=length(which(rowSums(abs(cbind(0,libMat[i,i0]-libMat[-c(i),i0])))==0))
	}
	if(length(i1)>0){
		peerAgreementLib[i,2]=length(which(rowSums(abs(cbind(0,libMat[i,i1]-libMat[-c(i),i1])))==0))
	}
}

# sort of silly way to show that usually more people in party that agree with you on your party consistent positions
# the more agree with you, attitudes are correlated with more weight in the estimator, ...

# a) most illuminating here
# c) simulations of support
vote=candidate_matrix


libMat=cbind(
	as.numeric(vote$libcon_envs<0),as.numeric(vote$libcon_guns<0),
	as.numeric(vote$libcon_taxs<0),as.numeric(vote$libcon_gays<0),
	as.numeric(vote$libcon_trad<0),
	as.numeric(vote$libcon_need<0),
	as.numeric(vote$libcon_immi<0),as.numeric(vote$libcon_abrt<0),
	as.numeric(vote$libcon_dfns<0),
	as.numeric(vote$libcon_crme<0)
)

conMat=cbind(
	as.numeric(vote$libcon_envs>0),as.numeric(vote$libcon_guns>0),
	as.numeric(vote$libcon_taxs>0),as.numeric(vote$libcon_gays>0),
	as.numeric(vote$libcon_trad>0),
	as.numeric(vote$libcon_need>0),
	as.numeric(vote$libcon_immi>0),as.numeric(vote$libcon_abrt>0),
	as.numeric(vote$libcon_dfns>0),
	as.numeric(vote$libcon_crme>0)
)
colnames(conMat)=colnames(libMat)=c(
'libcon_envs','libcon_guns',
'libcon_taxs','libcon_gays',
'libcon_trad',
'libcon_need',
'libcon_immi','libcon_abrt',
'libcon_dfns',
'libcon_crme')

libPos=rowSums(libMat)
conPos=rowSums(conMat)

table(conPos[which(vote$pid==-1)])/sum(table(conPos[which(vote$pid==-1)]))
table(libPos[which(vote$pid==1)])/sum(table(libPos[which(vote$pid==1)]))
pid=vote$pid


vote$agree =
vote$"ii_need" +
vote$"ii_govabuse" +
vote$"ii_reducemilitary" +
vote$"ii_strengthenmilitary" +
vote$"ii_raisetaxes" +
vote$"ii_cuttaxes" +
vote$"ii_lgbt" +
vote$"ii_marriage" +
vote$"ii_co2emissions" +
vote$"ii_drilling" +
vote$"ii_freetrade" +
vote$"ii_unfairtrade" +
vote$"ii_righttochoose" +
vote$"ii_unbornlives" +
vote$"ii_guncontrol" +
vote$"ii_gunrights" +
vote$"ii_citizenship" +
vote$"ii_bordersecurity" +
vote$"ii_policing" +
vote$"ii_criminals"

voteA=vote[which(vote$candidate=='A'),]
voteB=vote[which(vote$candidate=='B'),]
voteA$y_choice=sample(c(0,1),nrow(voteA),replace=T)

voteA$y_choice[which(voteA$agree>voteB$agree)]=1
voteA$y_choice[which(voteA$agree<voteB$agree)]=0
voteB$y_choice=1-voteA$y_choice
voteC=rbind(voteA,voteB)


reps <- subset(voteC, voteC$pid3==1)
dems <- subset(voteC, voteC$pid3==-1)
inds <- subset(voteC, voteC$pid3==-1)

attach(reps)
primary_elec_reps <- lm(y_choice~
pty + g_female+
re_black+re_hispanic+
r_catholic+r_evangelical+r_protestant+
o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
rec_refuse+rec_secure+rec_stand+rec_work+
i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
weights=1/wt)

primary_elec_reps_clse <- clse.f(reps,primary_elec_reps,respondent)
detach(reps)

attach(dems)
primary_elec_dems <- lm(y_choice~
pty + g_female+
re_black+re_hispanic+
r_catholic+r_evangelical+r_protestant+
o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
rec_refuse+rec_secure+rec_stand+rec_work+
i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
weights = 1/wt)

primary_elec_dems_clse <- clse.f(dems,primary_elec_dems,respondent)
detach(dems)

primary_elec_inds=primary_elec_dems
primary_elec_inds_clse =primary_elec_dems_clse


results_matrix <- cbind(primary_elec_dems$coefficients,primary_elec_inds$coefficients,primary_elec_reps$coefficients)
# remove pty coef
results_matrix=results_matrix[-c(2),]
results_matrix=cbind(results_matrix,NA,NA,NA)

for(j in 1:nrow(results_matrix)){
	ix=which(names(primary_elec_dems_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,4]=primary_elec_dems_clse[ix,2]
	}

	ix=which(names(primary_elec_inds_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,5]=primary_elec_inds_clse[ix,2]
	}

	ix=which(names(primary_elec_reps_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,6]=primary_elec_reps_clse[ix,2]
	}
}


results_matrix <- results_matrix[,c(1,4,2,5,3,6)]

colnames(results_matrix) <- c("d_estimate","d_se","i_estimate","i_se","r_estimate","r_se")
results_matrix <- results_matrix[-c(1),]
results_matrix_withnames <- cbind(var=rownames(results_matrix),results_matrix)
rownames(results_matrix_withnames) <- seq(1,57,by=1)

#results_matrix_withnames[is.na(results_matrix_withnames)]<- 0
##Now inserting rows for the omitted levels

#omitted: male, white, no religion, attorney, decent, newspaper endorsements, record = helping constituents, raising taxes for both issues

g_male <- c("g_male",rep(0,6))
re_white <- c("re_white",rep(0,6))
r_none <- c("r_none",rep(0,6))
o_attorney <- c("o_attorney",rep(0,6))
p_decent <- c("p_decent",rep(0,6))
e_newspapers <- c("e_newspapers",rep(0,6))
rec_help <- c("rec_help",rep(0,6))
#i_raisetaxes <- c("i1_raisetaxes",rep(0,6))
i_freetrade <- c("i1_freetrade",rep(0,6))

full_matrix <- rbind(
g_male,
results_matrix_withnames[1,],
re_white,
results_matrix_withnames[2:3,],
r_none,
results_matrix_withnames[4:6,],
o_attorney,
results_matrix_withnames[7:15,],
p_decent,
results_matrix_withnames[16:22,],
e_newspapers,
results_matrix_withnames[23:34,],
rec_help,
results_matrix_withnames[35:38,],
i_freetrade,
results_matrix_withnames[39:57,]
)

rownames(full_matrix) <- full_matrix[,1]
full_matrix <- full_matrix[,2:ncol(full_matrix)]
full_matrix_clean <- apply(full_matrix,2,as.numeric)
rownames(full_matrix_clean) <- rownames(full_matrix)

labels <- c(
"Gender - Male",
"Gender - Female",
"Race - White",
"Race - Black",
"Race - Hispanic",
"Religion - None",
"Religion - Catholic",
"Religion - Evangelical Protestant",
"Religion - Protestant",
"Occupation - Attorney",
"Occupation - CEO",
"Occupation - City Council Member",
"Occupation - Factory Foreman",
"Occupation - Farmer",
"Occupation - Former US Army Major",
"Occupation - Political Staffer",
"Occupation - Small Business Owner",
"Occupation - State Legislator",
"Occupation - Teacher",
"Personality - Decent",
"Personality - Compassionate",
"Personality - Empathetic",
"Personality - Inspiring",
"Personality - Intelligent",
"Personality - Knowledgeable",
"Personality - Moral",
"Personality - Strong Leader",
"Endorsements - Major area newspapers",
"Endorsements - Business groups",
"Endorsements - Christian groups",
"Endorsements - Civil rights groups",
"Endorsements - Energy groups",
"Endorsements - Environmental groups",
"Endorsements - Gun control groups",
"Endorsements - Gun rights groups",
"Endorsements - Labor unions",
"Endorsements - Reproductive rights groups",
"Endorsements - Tax reform groups",
"Endorsements - Tea Party groups",
"Endorsements - Veterans groups",
"Record - Help my constituents get the benefits they deserve",
"Record - Refuse to compromise my principles even when it means taking on my party",
"Record - Secure appointment to a powerful legislative committee",
"Record - Stand with my party to do what's right",
"Record - Work across the aisle to get things done",
"Issue - Promote expanding free trade agreements",
"Issue - Raise taxes on those making more than $250,000 a year",
"Issue - Cut taxes on income and capital gains for all",
"Issue - Defend the rights of LGBT individuals",
"Issue - Defend traditional marriage and religious beliefs",
"Issue - Expand domestic oil and gas production through drilling",
"Issue - Expand government and unemployment assistance for those in need",
"Issue - Prevent and prosecute abuse of government assistance programs",
"Issue - Protect a woman's right to choose",
"Issue - Protect gun owners' rights to defend themselves and others",
"Issue - Protect jobs and industry from unfair foreign trade",
"Issue - Protect the lives of the unborn",
"Issue - Provide a path to citizenship for undocumented immigrants",
"Issue - Reduce the size of military and number of military bases",
"Issue - Reform policing and stop racial profiling",
"Issue - Regulate CO2 emissions to combat global warming",
"Issue - Strengthen border security to stop illegal immigration",
"Issue - Strengthen gun control through commonsense restrictions",
"Issue - Strengthen our military and national defense",
"Issue - Toughen sentences and penalties for criminals")

core_for_ggplot <- data.frame(labels=labels,full_matrix_clean)
core_for_ggplot$iv_order <- factor(core_for_ggplot$labels, as.character(core_for_ggplot$labels))

omits=c("Gender - Male",
"Race - White",
"Religion - None",
"Occupation - Attorney",
"Personality - Decent",
"Endorsements - Major area newspapers",
"Record - Help my constituents get the benefits they deserve",
"Issue - Promote expanding free trade agreements")

label.groups=str_sub(labels,1,str_locate(labels,pattern='-')[,1]-2)
o.column=grep(names(core_for_ggplot),pattern='i_est')

#core_for_ggplot=lableOrder(xmat=core_for_ggplot,labels,label.groups,omits,o.column)
#write.csv(core_for_ggplot,"csv/choice_primary_core_for_ggplot.csv")

###########################################
###Then, plot those models

#Doing the within-factor sorting in shitty hack
#core_for_ggplot <- read.csv("csv/choice_primary_core_for_ggplot.csv")
o.order <- read.csv("data/core_for_ggplot_global.csv")$iv_order

core_for_ggplot=reOrder(x=core_for_ggplot,o=o.order)

# augment w/ scores bivariate coefficient || excluded ...

# ideology
primary_elec_dems_ideo <- lm(data=dems,dv_choice~
pty + scores,weights = 1/wt)
primary_elec_dems_ideo_clse <- clse.f(dems,primary_elec_dems_ideo,dems$respondent)

primary_elec_reps_ideo <- lm(data=reps,dv_choice~
pty + scores,weights = 1/wt)
primary_elec_reps_ideo_clse <- clse.f(reps,primary_elec_reps_ideo,reps$respondent)

primary_elec_inds_ideo <- lm(data=inds,dv_choice~
pty + scores,weights = 1/wt)
primary_elec_inds_ideo_clse <- clse.f(inds,primary_elec_inds_ideo,inds$respondent)

core_for_ggplot[,1]=as.character(core_for_ggplot[,1])
#core_for_ggplot[,2]=as.character(core_for_ggplot[,2])
core_for_ggplot[,8]=as.character(core_for_ggplot[,8])
#core_for_ggplot=rbind(core_for_ggplot,c('i_ideology','Overall Ideology Rating',
	#primary_elec_dems_ideo_clse[2,1],primary_elec_dems_ideo_clse[2,2],
	#primary_elec_inds_ideo_clse[2,1],primary_elec_inds_ideo_clse[2,2],
	#primary_elec_reps_ideo_clse[2,1],primary_elec_reps_ideo_clse[2,2],
	#'Overall Ideology Rating'))

attach(core_for_ggplot)
labels=core_for_ggplot$labels


ggplot_stacked <- data.frame(
labels=c(as.character(labels), as.character(labels), as.character(labels)),
PID=c(rep("Democrat",65),rep("Independent",65),rep("Republican",65)),
estimate=c(d_estimate,i_estimate,r_estimate),
se=c(d_se,i_se,r_se))

#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

ggplot_stacked=ggplot_stacked[!grepl(ggplot_stacked$PID,pattern='Independent'),]
#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

detach(core_for_ggplot)
attach(ggplot_stacked)


ggplot_stacked$estimate=as.numeric(as.character(ggplot_stacked$estimate))
ggplot_stacked$se=as.numeric(as.character(ggplot_stacked$se))

#exclude overall ideology
#ggplot_stacked=ggplot_stacked[-c(grep(as.character(ggplot_stacked[,1]),pattern='Overall')),]
ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
#write.csv(ggplot_stacked,"csv/choice_primary_ggplot_stacked.csv")
ggplot_stacked=ggplot_stacked[grep(ggplot_stacked[,1],pattern='Issue'),]

attach(ggplot_stacked)
labels=unique(ggplot_stacked$labels)

# choice_primary.R
primary_ggplot_stacked=ggplot_stacked

pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Democrat',replace=paste(sep='','Democrat (N=',length(unique(dems$respondent)),')'))
ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Republican',replace=paste(sep='','Republican (N=',length(unique(reps$respondent)),')'))

pdf("appendix/figures/agreement_simulation_all.pdf", width=8, height=5, pointsize=1)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.45,0.45) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(5.75,5.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=25.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=38.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()




vote$agreeL =
vote$"ii_need" +
#vote$"ii_govabuse" +
vote$"ii_reducemilitary" +
#vote$"ii_strengthenmilitary" +
vote$"ii_raisetaxes" +
#vote$"ii_cuttaxes" +
vote$"ii_lgbt" +
#vote$"ii_marriage" +
vote$"ii_co2emissions" +
#vote$"ii_drilling" +
vote$"ii_freetrade" +
#vote$"ii_unfairtrade" +
vote$"ii_righttochoose" +
#vote$"ii_unbornlives" +
vote$"ii_guncontrol" +
#vote$"ii_gunrights" +
vote$"ii_citizenship" +
#vote$"ii_bordersecurity" +
vote$"ii_policing" #+
#vote$"ii_criminals"


vote$agreeR =
#vote$"ii_need" +
vote$"ii_govabuse" +
#vote$"ii_reducemilitary" +
vote$"ii_strengthenmilitary" +
#vote$"ii_raisetaxes" +
vote$"ii_cuttaxes" +
#vote$"ii_lgbt" +
vote$"ii_marriage" +
#vote$"ii_co2emissions" +
vote$"ii_drilling" +
#vote$"ii_freetrade" +
vote$"ii_unfairtrade" +
#vote$"ii_righttochoose" +
vote$"ii_unbornlives" +
#vote$"ii_guncontrol" +
vote$"ii_gunrights" +
#vote$"ii_citizenship" +
vote$"ii_bordersecurity" +
#vote$"ii_policing" +
vote$"ii_criminals"


voteA=vote[which(vote$candidate=='A'),]
voteB=vote[which(vote$candidate=='B'),]
voteA$y_choice=sample(c(0,1),nrow(voteA),replace=T)

voteA$y_choice[which(voteA$agreeL>voteB$agreeL & voteA$pid==-1)]=1
voteA$y_choice[which(voteA$agreeL<voteB$agreeL & voteA$pid==-1)]=0
voteA$y_choice[which(voteA$agreeR>voteB$agreeR & voteA$pid==1)]=1
voteA$y_choice[which(voteA$agreeR<voteB$agreeR & voteA$pid==1)]=0

voteB$y_choice=1-voteA$y_choice
voteC=rbind(voteA,voteB)


reps <- subset(voteC, voteC$pid3==1)
dems <- subset(voteC, voteC$pid3==-1)
inds <- subset(voteC, voteC$pid3==-1)

attach(reps)
primary_elec_reps <- lm(y_choice~
pty + g_female+
re_black+re_hispanic+
r_catholic+r_evangelical+r_protestant+
o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
rec_refuse+rec_secure+rec_stand+rec_work+
i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
weights=1/wt)

primary_elec_reps_clse <- clse.f(reps,primary_elec_reps,respondent)
detach(reps)

attach(dems)
primary_elec_dems <- lm(y_choice~
pty + g_female+
re_black+re_hispanic+
r_catholic+r_evangelical+r_protestant+
o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
rec_refuse+rec_secure+rec_stand+rec_work+
i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
weights = 1/wt)

primary_elec_dems_clse <- clse.f(dems,primary_elec_dems,respondent)
detach(dems)

primary_elec_inds=primary_elec_dems
primary_elec_inds_clse =primary_elec_dems_clse


results_matrix <- cbind(primary_elec_dems$coefficients,primary_elec_inds$coefficients,primary_elec_reps$coefficients)
# remove pty coef
results_matrix=results_matrix[-c(2),]
results_matrix=cbind(results_matrix,NA,NA,NA)

for(j in 1:nrow(results_matrix)){
	ix=which(names(primary_elec_dems_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,4]=primary_elec_dems_clse[ix,2]
	}

	ix=which(names(primary_elec_inds_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,5]=primary_elec_inds_clse[ix,2]
	}

	ix=which(names(primary_elec_reps_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,6]=primary_elec_reps_clse[ix,2]
	}
}


results_matrix <- results_matrix[,c(1,4,2,5,3,6)]

colnames(results_matrix) <- c("d_estimate","d_se","i_estimate","i_se","r_estimate","r_se")
results_matrix <- results_matrix[-c(1),]
results_matrix_withnames <- cbind(var=rownames(results_matrix),results_matrix)
rownames(results_matrix_withnames) <- seq(1,57,by=1)

#results_matrix_withnames[is.na(results_matrix_withnames)]<- 0
##Now inserting rows for the omitted levels

#omitted: male, white, no religion, attorney, decent, newspaper endorsements, record = helping constituents, raising taxes for both issues

g_male <- c("g_male",rep(0,6))
re_white <- c("re_white",rep(0,6))
r_none <- c("r_none",rep(0,6))
o_attorney <- c("o_attorney",rep(0,6))
p_decent <- c("p_decent",rep(0,6))
e_newspapers <- c("e_newspapers",rep(0,6))
rec_help <- c("rec_help",rep(0,6))
#i_raisetaxes <- c("i1_raisetaxes",rep(0,6))
i_freetrade <- c("i1_freetrade",rep(0,6))

full_matrix <- rbind(
g_male,
results_matrix_withnames[1,],
re_white,
results_matrix_withnames[2:3,],
r_none,
results_matrix_withnames[4:6,],
o_attorney,
results_matrix_withnames[7:15,],
p_decent,
results_matrix_withnames[16:22,],
e_newspapers,
results_matrix_withnames[23:34,],
rec_help,
results_matrix_withnames[35:38,],
i_freetrade,
results_matrix_withnames[39:57,]
)

rownames(full_matrix) <- full_matrix[,1]
full_matrix <- full_matrix[,2:ncol(full_matrix)]
full_matrix_clean <- apply(full_matrix,2,as.numeric)
rownames(full_matrix_clean) <- rownames(full_matrix)

labels <- c(
"Gender - Male",
"Gender - Female",
"Race - White",
"Race - Black",
"Race - Hispanic",
"Religion - None",
"Religion - Catholic",
"Religion - Evangelical Protestant",
"Religion - Protestant",
"Occupation - Attorney",
"Occupation - CEO",
"Occupation - City Council Member",
"Occupation - Factory Foreman",
"Occupation - Farmer",
"Occupation - Former US Army Major",
"Occupation - Political Staffer",
"Occupation - Small Business Owner",
"Occupation - State Legislator",
"Occupation - Teacher",
"Personality - Decent",
"Personality - Compassionate",
"Personality - Empathetic",
"Personality - Inspiring",
"Personality - Intelligent",
"Personality - Knowledgeable",
"Personality - Moral",
"Personality - Strong Leader",
"Endorsements - Major area newspapers",
"Endorsements - Business groups",
"Endorsements - Christian groups",
"Endorsements - Civil rights groups",
"Endorsements - Energy groups",
"Endorsements - Environmental groups",
"Endorsements - Gun control groups",
"Endorsements - Gun rights groups",
"Endorsements - Labor unions",
"Endorsements - Reproductive rights groups",
"Endorsements - Tax reform groups",
"Endorsements - Tea Party groups",
"Endorsements - Veterans groups",
"Record - Help my constituents get the benefits they deserve",
"Record - Refuse to compromise my principles even when it means taking on my party",
"Record - Secure appointment to a powerful legislative committee",
"Record - Stand with my party to do what's right",
"Record - Work across the aisle to get things done",
"Issue - Promote expanding free trade agreements",
"Issue - Raise taxes on those making more than $250,000 a year",
"Issue - Cut taxes on income and capital gains for all",
"Issue - Defend the rights of LGBT individuals",
"Issue - Defend traditional marriage and religious beliefs",
"Issue - Expand domestic oil and gas production through drilling",
"Issue - Expand government and unemployment assistance for those in need",
"Issue - Prevent and prosecute abuse of government assistance programs",
"Issue - Protect a woman's right to choose",
"Issue - Protect gun owners' rights to defend themselves and others",
"Issue - Protect jobs and industry from unfair foreign trade",
"Issue - Protect the lives of the unborn",
"Issue - Provide a path to citizenship for undocumented immigrants",
"Issue - Reduce the size of military and number of military bases",
"Issue - Reform policing and stop racial profiling",
"Issue - Regulate CO2 emissions to combat global warming",
"Issue - Strengthen border security to stop illegal immigration",
"Issue - Strengthen gun control through commonsense restrictions",
"Issue - Strengthen our military and national defense",
"Issue - Toughen sentences and penalties for criminals")

core_for_ggplot <- data.frame(labels=labels,full_matrix_clean)
core_for_ggplot$iv_order <- factor(core_for_ggplot$labels, as.character(core_for_ggplot$labels))

omits=c("Gender - Male",
"Race - White",
"Religion - None",
"Occupation - Attorney",
"Personality - Decent",
"Endorsements - Major area newspapers",
"Record - Help my constituents get the benefits they deserve",
"Issue - Promote expanding free trade agreements")

label.groups=str_sub(labels,1,str_locate(labels,pattern='-')[,1]-2)
o.column=grep(names(core_for_ggplot),pattern='i_est')

#core_for_ggplot=lableOrder(xmat=core_for_ggplot,labels,label.groups,omits,o.column)
#write.csv(core_for_ggplot,"csv/choice_primary_core_for_ggplot.csv")

###########################################
###Then, plot those models

#Doing the within-factor sorting in shitty hack
#core_for_ggplot <- read.csv("csv/choice_primary_core_for_ggplot.csv")
o.order <- read.csv("data/core_for_ggplot_global.csv")$iv_order

core_for_ggplot=reOrder(x=core_for_ggplot,o=o.order)

# augment w/ scores bivariate coefficient || excluded ...

# ideology
primary_elec_dems_ideo <- lm(data=dems,dv_choice~
pty + scores,weights = 1/wt)
primary_elec_dems_ideo_clse <- clse.f(dems,primary_elec_dems_ideo,dems$respondent)

primary_elec_reps_ideo <- lm(data=reps,dv_choice~
pty + scores,weights = 1/wt)
primary_elec_reps_ideo_clse <- clse.f(reps,primary_elec_reps_ideo,reps$respondent)

primary_elec_inds_ideo <- lm(data=inds,dv_choice~
pty + scores,weights = 1/wt)
primary_elec_inds_ideo_clse <- clse.f(inds,primary_elec_inds_ideo,inds$respondent)

core_for_ggplot[,1]=as.character(core_for_ggplot[,1])
#core_for_ggplot[,2]=as.character(core_for_ggplot[,2])
core_for_ggplot[,8]=as.character(core_for_ggplot[,8])
#core_for_ggplot=rbind(core_for_ggplot,c('i_ideology','Overall Ideology Rating',
	#primary_elec_dems_ideo_clse[2,1],primary_elec_dems_ideo_clse[2,2],
	#primary_elec_inds_ideo_clse[2,1],primary_elec_inds_ideo_clse[2,2],
	#primary_elec_reps_ideo_clse[2,1],primary_elec_reps_ideo_clse[2,2],
	#'Overall Ideology Rating'))

attach(core_for_ggplot)
labels=core_for_ggplot$labels


ggplot_stacked <- data.frame(
labels=c(as.character(labels), as.character(labels), as.character(labels)),
PID=c(rep("Democrat",65),rep("Independent",65),rep("Republican",65)),
estimate=c(d_estimate,i_estimate,r_estimate),
se=c(d_se,i_se,r_se))

#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

ggplot_stacked=ggplot_stacked[!grepl(ggplot_stacked$PID,pattern='Independent'),]
#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

detach(core_for_ggplot)
attach(ggplot_stacked)


ggplot_stacked$estimate=as.numeric(as.character(ggplot_stacked$estimate))
ggplot_stacked$se=as.numeric(as.character(ggplot_stacked$se))

#exclude overall ideology
#ggplot_stacked=ggplot_stacked[-c(grep(as.character(ggplot_stacked[,1]),pattern='Overall')),]
ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
#write.csv(ggplot_stacked,"csv/choice_primary_ggplot_stacked.csv")
ggplot_stacked=ggplot_stacked[grep(ggplot_stacked[,1],pattern='Issue'),]

attach(ggplot_stacked)
labels=unique(ggplot_stacked$labels)

# choice_primary.R
primary_ggplot_stacked=ggplot_stacked

pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Democrat',replace=paste(sep='','Democrat (N=',length(unique(dems$respondent)),')'))
ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Republican',replace=paste(sep='','Republican (N=',length(unique(reps$respondent)),')'))



pdf("appendix/figures/agreement_simulation_consistent.pdf", width=8, height=5, pointsize=1)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.45,0.45) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(5.75,5.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=25.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=38.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()



# consistency given more weight
w=.52
vote$agreeL =
w*vote$"ii_need" +
(1-w)*vote$"ii_govabuse" +
w*vote$"ii_reducemilitary" +
(1-w)*vote$"ii_strengthenmilitary" +
w*vote$"ii_raisetaxes" +
(1-w)*vote$"ii_cuttaxes" +
w*vote$"ii_lgbt" +
(1-w)*vote$"ii_marriage" +
w*vote$"ii_co2emissions" +
(1-w)*vote$"ii_drilling" +
w*vote$"ii_freetrade" +
(1-w)*vote$"ii_unfairtrade" +
w*vote$"ii_righttochoose" +
(1-w)*vote$"ii_unbornlives" +
w*vote$"ii_guncontrol" +
(1-w)*vote$"ii_gunrights" +
w*vote$"ii_citizenship" +
(1-w)*vote$"ii_bordersecurity" +
w*vote$"ii_policing" +
(1-w)*vote$"ii_criminals"


vote$agreeR =
(1-w)*vote$"ii_need" +
w*vote$"ii_govabuse" +
(1-w)*vote$"ii_reducemilitary" +
w*vote$"ii_strengthenmilitary" +
(1-w)*vote$"ii_raisetaxes" +
w*vote$"ii_cuttaxes" +
(1-w)*vote$"ii_lgbt" +
w*vote$"ii_marriage" +
(1-w)*vote$"ii_co2emissions" +
w*vote$"ii_drilling" +
(1-w)*vote$"ii_freetrade" +
w*vote$"ii_unfairtrade" +
(1-w)*vote$"ii_righttochoose" +
w*vote$"ii_unbornlives" +
(1-w)*vote$"ii_guncontrol" +
w*vote$"ii_gunrights" +
(1-w)*vote$"ii_citizenship" +
w*vote$"ii_bordersecurity" +
(1-w)*vote$"ii_policing" +
w*vote$"ii_criminals"


voteA=vote[which(vote$candidate=='A'),]
voteB=vote[which(vote$candidate=='B'),]
voteA$y_choice=sample(c(0,1),nrow(voteA),replace=T)

voteA$y_choice[which(voteA$agreeL>voteB$agreeL & voteA$pid==-1)]=1
voteA$y_choice[which(voteA$agreeL<voteB$agreeL & voteA$pid==-1)]=0
voteA$y_choice[which(voteA$agreeR>voteB$agreeR & voteA$pid==1)]=1
voteA$y_choice[which(voteA$agreeR<voteB$agreeR & voteA$pid==1)]=0

voteB$y_choice=1-voteA$y_choice
voteC=rbind(voteA,voteB)


reps <- subset(voteC, voteC$pid3==1)
dems <- subset(voteC, voteC$pid3==-1)
inds <- subset(voteC, voteC$pid3==-1)

attach(reps)
primary_elec_reps <- lm(y_choice~
pty + g_female+
re_black+re_hispanic+
r_catholic+r_evangelical+r_protestant+
o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
rec_refuse+rec_secure+rec_stand+rec_work+
i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
weights=1/wt)

primary_elec_reps_clse <- clse.f(reps,primary_elec_reps,respondent)
detach(reps)

attach(dems)
primary_elec_dems <- lm(y_choice~
pty + g_female+
re_black+re_hispanic+
r_catholic+r_evangelical+r_protestant+
o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
rec_refuse+rec_secure+rec_stand+rec_work+
i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
weights = 1/wt)

primary_elec_dems_clse <- clse.f(dems,primary_elec_dems,respondent)
detach(dems)

primary_elec_inds=primary_elec_dems
primary_elec_inds_clse =primary_elec_dems_clse


results_matrix <- cbind(primary_elec_dems$coefficients,primary_elec_inds$coefficients,primary_elec_reps$coefficients)
# remove pty coef
results_matrix=results_matrix[-c(2),]
results_matrix=cbind(results_matrix,NA,NA,NA)

for(j in 1:nrow(results_matrix)){
	ix=which(names(primary_elec_dems_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,4]=primary_elec_dems_clse[ix,2]
	}

	ix=which(names(primary_elec_inds_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,5]=primary_elec_inds_clse[ix,2]
	}

	ix=which(names(primary_elec_reps_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,6]=primary_elec_reps_clse[ix,2]
	}
}


results_matrix <- results_matrix[,c(1,4,2,5,3,6)]

colnames(results_matrix) <- c("d_estimate","d_se","i_estimate","i_se","r_estimate","r_se")
results_matrix <- results_matrix[-c(1),]
results_matrix_withnames <- cbind(var=rownames(results_matrix),results_matrix)
rownames(results_matrix_withnames) <- seq(1,57,by=1)

#results_matrix_withnames[is.na(results_matrix_withnames)]<- 0
##Now inserting rows for the omitted levels

#omitted: male, white, no religion, attorney, decent, newspaper endorsements, record = helping constituents, raising taxes for both issues

g_male <- c("g_male",rep(0,6))
re_white <- c("re_white",rep(0,6))
r_none <- c("r_none",rep(0,6))
o_attorney <- c("o_attorney",rep(0,6))
p_decent <- c("p_decent",rep(0,6))
e_newspapers <- c("e_newspapers",rep(0,6))
rec_help <- c("rec_help",rep(0,6))
#i_raisetaxes <- c("i1_raisetaxes",rep(0,6))
i_freetrade <- c("i1_freetrade",rep(0,6))

full_matrix <- rbind(
g_male,
results_matrix_withnames[1,],
re_white,
results_matrix_withnames[2:3,],
r_none,
results_matrix_withnames[4:6,],
o_attorney,
results_matrix_withnames[7:15,],
p_decent,
results_matrix_withnames[16:22,],
e_newspapers,
results_matrix_withnames[23:34,],
rec_help,
results_matrix_withnames[35:38,],
i_freetrade,
results_matrix_withnames[39:57,]
)

rownames(full_matrix) <- full_matrix[,1]
full_matrix <- full_matrix[,2:ncol(full_matrix)]
full_matrix_clean <- apply(full_matrix,2,as.numeric)
rownames(full_matrix_clean) <- rownames(full_matrix)

labels <- c(
"Gender - Male",
"Gender - Female",
"Race - White",
"Race - Black",
"Race - Hispanic",
"Religion - None",
"Religion - Catholic",
"Religion - Evangelical Protestant",
"Religion - Protestant",
"Occupation - Attorney",
"Occupation - CEO",
"Occupation - City Council Member",
"Occupation - Factory Foreman",
"Occupation - Farmer",
"Occupation - Former US Army Major",
"Occupation - Political Staffer",
"Occupation - Small Business Owner",
"Occupation - State Legislator",
"Occupation - Teacher",
"Personality - Decent",
"Personality - Compassionate",
"Personality - Empathetic",
"Personality - Inspiring",
"Personality - Intelligent",
"Personality - Knowledgeable",
"Personality - Moral",
"Personality - Strong Leader",
"Endorsements - Major area newspapers",
"Endorsements - Business groups",
"Endorsements - Christian groups",
"Endorsements - Civil rights groups",
"Endorsements - Energy groups",
"Endorsements - Environmental groups",
"Endorsements - Gun control groups",
"Endorsements - Gun rights groups",
"Endorsements - Labor unions",
"Endorsements - Reproductive rights groups",
"Endorsements - Tax reform groups",
"Endorsements - Tea Party groups",
"Endorsements - Veterans groups",
"Record - Help my constituents get the benefits they deserve",
"Record - Refuse to compromise my principles even when it means taking on my party",
"Record - Secure appointment to a powerful legislative committee",
"Record - Stand with my party to do what's right",
"Record - Work across the aisle to get things done",
"Issue - Promote expanding free trade agreements",
"Issue - Raise taxes on those making more than $250,000 a year",
"Issue - Cut taxes on income and capital gains for all",
"Issue - Defend the rights of LGBT individuals",
"Issue - Defend traditional marriage and religious beliefs",
"Issue - Expand domestic oil and gas production through drilling",
"Issue - Expand government and unemployment assistance for those in need",
"Issue - Prevent and prosecute abuse of government assistance programs",
"Issue - Protect a woman's right to choose",
"Issue - Protect gun owners' rights to defend themselves and others",
"Issue - Protect jobs and industry from unfair foreign trade",
"Issue - Protect the lives of the unborn",
"Issue - Provide a path to citizenship for undocumented immigrants",
"Issue - Reduce the size of military and number of military bases",
"Issue - Reform policing and stop racial profiling",
"Issue - Regulate CO2 emissions to combat global warming",
"Issue - Strengthen border security to stop illegal immigration",
"Issue - Strengthen gun control through commonsense restrictions",
"Issue - Strengthen our military and national defense",
"Issue - Toughen sentences and penalties for criminals")

core_for_ggplot <- data.frame(labels=labels,full_matrix_clean)
core_for_ggplot$iv_order <- factor(core_for_ggplot$labels, as.character(core_for_ggplot$labels))

omits=c("Gender - Male",
"Race - White",
"Religion - None",
"Occupation - Attorney",
"Personality - Decent",
"Endorsements - Major area newspapers",
"Record - Help my constituents get the benefits they deserve",
"Issue - Promote expanding free trade agreements")

label.groups=str_sub(labels,1,str_locate(labels,pattern='-')[,1]-2)
o.column=grep(names(core_for_ggplot),pattern='i_est')

#core_for_ggplot=lableOrder(xmat=core_for_ggplot,labels,label.groups,omits,o.column)
#write.csv(core_for_ggplot,"csv/choice_primary_core_for_ggplot.csv")

###########################################
###Then, plot those models

#Doing the within-factor sorting in shitty hack
#core_for_ggplot <- read.csv("csv/choice_primary_core_for_ggplot.csv")
o.order <- read.csv("data/core_for_ggplot_global.csv")$iv_order

core_for_ggplot=reOrder(x=core_for_ggplot,o=o.order)

# augment w/ scores bivariate coefficient || excluded ...

# ideology
primary_elec_dems_ideo <- lm(data=dems,dv_choice~
pty + scores,weights = 1/wt)
primary_elec_dems_ideo_clse <- clse.f(dems,primary_elec_dems_ideo,dems$respondent)

primary_elec_reps_ideo <- lm(data=reps,dv_choice~
pty + scores,weights = 1/wt)
primary_elec_reps_ideo_clse <- clse.f(reps,primary_elec_reps_ideo,reps$respondent)

primary_elec_inds_ideo <- lm(data=inds,dv_choice~
pty + scores,weights = 1/wt)
primary_elec_inds_ideo_clse <- clse.f(inds,primary_elec_inds_ideo,inds$respondent)

core_for_ggplot[,1]=as.character(core_for_ggplot[,1])
#core_for_ggplot[,2]=as.character(core_for_ggplot[,2])
core_for_ggplot[,8]=as.character(core_for_ggplot[,8])
#core_for_ggplot=rbind(core_for_ggplot,c('i_ideology','Overall Ideology Rating',
	#primary_elec_dems_ideo_clse[2,1],primary_elec_dems_ideo_clse[2,2],
	#primary_elec_inds_ideo_clse[2,1],primary_elec_inds_ideo_clse[2,2],
	#primary_elec_reps_ideo_clse[2,1],primary_elec_reps_ideo_clse[2,2],
	#'Overall Ideology Rating'))

attach(core_for_ggplot)
labels=core_for_ggplot$labels


ggplot_stacked <- data.frame(
labels=c(as.character(labels), as.character(labels), as.character(labels)),
PID=c(rep("Democrat",65),rep("Independent",65),rep("Republican",65)),
estimate=c(d_estimate,i_estimate,r_estimate),
se=c(d_se,i_se,r_se))

#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

ggplot_stacked=ggplot_stacked[!grepl(ggplot_stacked$PID,pattern='Independent'),]
#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

detach(core_for_ggplot)
attach(ggplot_stacked)


ggplot_stacked$estimate=as.numeric(as.character(ggplot_stacked$estimate))
ggplot_stacked$se=as.numeric(as.character(ggplot_stacked$se))

#exclude overall ideology
#ggplot_stacked=ggplot_stacked[-c(grep(as.character(ggplot_stacked[,1]),pattern='Overall')),]
ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
#write.csv(ggplot_stacked,"csv/choice_primary_ggplot_stacked.csv")
ggplot_stacked=ggplot_stacked[grep(ggplot_stacked[,1],pattern='Issue'),]

attach(ggplot_stacked)
labels=unique(ggplot_stacked$labels)

# choice_primary.R
primary_ggplot_stacked=ggplot_stacked

pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Democrat',replace=paste(sep='','Democrat (N=',length(unique(dems$respondent)),')'))
ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Republican',replace=paste(sep='','Republican (N=',length(unique(reps$respondent)),')'))



pdf("appendix/figures/agreement_simulation_wgt.pdf", width=8, height=5, pointsize=1)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.45,0.45) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(5.75,5.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=25.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=38.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()


# load indices for sophistication :: replicate above given three-category sophisticate

load('data/sophistication_indices.Rdata')


L_ix2=indices$resp_id[indices$"L_ix2"]
M_ix2=indices$resp_id[indices$"M_ix2"]
H_ix2=indices$resp_id[indices$"H_ix2"]

vote$sophisticated=NA
for(i in 1:length(L_ix2)){
	iq=which(L_ix2[i]==vote$respondent)
	vote$sophisticated[iq]='L'
}
for(i in 1:length(M_ix2)){
	iq=which(M_ix2[i]==vote$respondent)
	vote$sophisticated[iq]='M'
}
for(i in 1:length(H_ix2)){
	iq=which(H_ix2[i]==vote$respondent)
	vote$sophisticated[iq]='H'
}

# consistency given more weight
w=.5
vote$agreeL =
w*vote$"ii_need" +
(1-w)*vote$"ii_govabuse" +
w*vote$"ii_reducemilitary" +
(1-w)*vote$"ii_strengthenmilitary" +
w*vote$"ii_raisetaxes" +
(1-w)*vote$"ii_cuttaxes" +
w*vote$"ii_lgbt" +
(1-w)*vote$"ii_marriage" +
w*vote$"ii_co2emissions" +
(1-w)*vote$"ii_drilling" +
w*vote$"ii_freetrade" +
(1-w)*vote$"ii_unfairtrade" +
w*vote$"ii_righttochoose" +
(1-w)*vote$"ii_unbornlives" +
w*vote$"ii_guncontrol" +
(1-w)*vote$"ii_gunrights" +
w*vote$"ii_citizenship" +
(1-w)*vote$"ii_bordersecurity" +
w*vote$"ii_policing" +
(1-w)*vote$"ii_criminals"


vote$agreeR =
(1-w)*vote$"ii_need" +
w*vote$"ii_govabuse" +
(1-w)*vote$"ii_reducemilitary" +
w*vote$"ii_strengthenmilitary" +
(1-w)*vote$"ii_raisetaxes" +
w*vote$"ii_cuttaxes" +
(1-w)*vote$"ii_lgbt" +
w*vote$"ii_marriage" +
(1-w)*vote$"ii_co2emissions" +
w*vote$"ii_drilling" +
(1-w)*vote$"ii_freetrade" +
w*vote$"ii_unfairtrade" +
(1-w)*vote$"ii_righttochoose" +
w*vote$"ii_unbornlives" +
(1-w)*vote$"ii_guncontrol" +
w*vote$"ii_gunrights" +
(1-w)*vote$"ii_citizenship" +
w*vote$"ii_bordersecurity" +
(1-w)*vote$"ii_policing" +
w*vote$"ii_criminals"


voteA=vote[which(vote$candidate=='A'),]
voteB=vote[which(vote$candidate=='B'),]
voteA$y_choice=sample(c(0,1),nrow(voteA),replace=T)

voteA$y_choice[which(voteA$agreeL>voteB$agreeL & voteA$pid==-1)]=1
voteA$y_choice[which(voteA$agreeL<voteB$agreeL & voteA$pid==-1)]=0
voteA$y_choice[which(voteA$agreeR>voteB$agreeR & voteA$pid==1)]=1
voteA$y_choice[which(voteA$agreeR<voteB$agreeR & voteA$pid==1)]=0

voteB$y_choice=1-voteA$y_choice
voteC=rbind(voteA,voteB)


reps <- subset(voteC, voteC$pid3==1)
dems <- subset(voteC, voteC$pid3==-1)
inds <- subset(voteC, voteC$pid3==-1)

i=0
i=i+1
mq=c('L','M','H')[i]
# low/medium/high
attach(reps)
primary_elec_reps <- lm(y_choice~
pty + g_female+
re_black+re_hispanic+
r_catholic+r_evangelical+r_protestant+
o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
rec_refuse+rec_secure+rec_stand+rec_work+
i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
weights=1/wt,subset=which(sophisticated==mq))

primary_elec_reps_clse <- clse.f(reps[which(sophisticated==mq),],primary_elec_reps,respondent[which(sophisticated==mq)])
detach(reps)

attach(dems)
primary_elec_dems <- lm(y_choice~
pty + g_female+
re_black+re_hispanic+
r_catholic+r_evangelical+r_protestant+
o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
rec_refuse+rec_secure+rec_stand+rec_work+
i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
weights=1/wt,subset=which(sophisticated==mq))

primary_elec_dems_clse <- clse.f(dems[which(sophisticated==mq),],primary_elec_dems,respondent[which(sophisticated==mq)])
detach(dems)

primary_elec_inds=primary_elec_dems
primary_elec_inds_clse =primary_elec_dems_clse


results_matrix <- cbind(primary_elec_dems$coefficients,primary_elec_inds$coefficients,primary_elec_reps$coefficients)
# remove pty coef
results_matrix=results_matrix[-c(2),]
results_matrix=cbind(results_matrix,NA,NA,NA)

for(j in 1:nrow(results_matrix)){
	ix=which(names(primary_elec_dems_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,4]=primary_elec_dems_clse[ix,2]
	}

	ix=which(names(primary_elec_inds_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,5]=primary_elec_inds_clse[ix,2]
	}

	ix=which(names(primary_elec_reps_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,6]=primary_elec_reps_clse[ix,2]
	}
}


results_matrix <- results_matrix[,c(1,4,2,5,3,6)]

colnames(results_matrix) <- c("d_estimate","d_se","i_estimate","i_se","r_estimate","r_se")
results_matrix <- results_matrix[-c(1),]
results_matrix_withnames <- cbind(var=rownames(results_matrix),results_matrix)
rownames(results_matrix_withnames) <- seq(1,57,by=1)

#results_matrix_withnames[is.na(results_matrix_withnames)]<- 0
##Now inserting rows for the omitted levels

#omitted: male, white, no religion, attorney, decent, newspaper endorsements, record = helping constituents, raising taxes for both issues

g_male <- c("g_male",rep(0,6))
re_white <- c("re_white",rep(0,6))
r_none <- c("r_none",rep(0,6))
o_attorney <- c("o_attorney",rep(0,6))
p_decent <- c("p_decent",rep(0,6))
e_newspapers <- c("e_newspapers",rep(0,6))
rec_help <- c("rec_help",rep(0,6))
#i_raisetaxes <- c("i1_raisetaxes",rep(0,6))
i_freetrade <- c("i1_freetrade",rep(0,6))

full_matrix <- rbind(
g_male,
results_matrix_withnames[1,],
re_white,
results_matrix_withnames[2:3,],
r_none,
results_matrix_withnames[4:6,],
o_attorney,
results_matrix_withnames[7:15,],
p_decent,
results_matrix_withnames[16:22,],
e_newspapers,
results_matrix_withnames[23:34,],
rec_help,
results_matrix_withnames[35:38,],
i_freetrade,
results_matrix_withnames[39:57,]
)

rownames(full_matrix) <- full_matrix[,1]
full_matrix <- full_matrix[,2:ncol(full_matrix)]
full_matrix_clean <- apply(full_matrix,2,as.numeric)
rownames(full_matrix_clean) <- rownames(full_matrix)

labels <- c(
"Gender - Male",
"Gender - Female",
"Race - White",
"Race - Black",
"Race - Hispanic",
"Religion - None",
"Religion - Catholic",
"Religion - Evangelical Protestant",
"Religion - Protestant",
"Occupation - Attorney",
"Occupation - CEO",
"Occupation - City Council Member",
"Occupation - Factory Foreman",
"Occupation - Farmer",
"Occupation - Former US Army Major",
"Occupation - Political Staffer",
"Occupation - Small Business Owner",
"Occupation - State Legislator",
"Occupation - Teacher",
"Personality - Decent",
"Personality - Compassionate",
"Personality - Empathetic",
"Personality - Inspiring",
"Personality - Intelligent",
"Personality - Knowledgeable",
"Personality - Moral",
"Personality - Strong Leader",
"Endorsements - Major area newspapers",
"Endorsements - Business groups",
"Endorsements - Christian groups",
"Endorsements - Civil rights groups",
"Endorsements - Energy groups",
"Endorsements - Environmental groups",
"Endorsements - Gun control groups",
"Endorsements - Gun rights groups",
"Endorsements - Labor unions",
"Endorsements - Reproductive rights groups",
"Endorsements - Tax reform groups",
"Endorsements - Tea Party groups",
"Endorsements - Veterans groups",
"Record - Help my constituents get the benefits they deserve",
"Record - Refuse to compromise my principles even when it means taking on my party",
"Record - Secure appointment to a powerful legislative committee",
"Record - Stand with my party to do what's right",
"Record - Work across the aisle to get things done",
"Issue - Promote expanding free trade agreements",
"Issue - Raise taxes on those making more than $250,000 a year",
"Issue - Cut taxes on income and capital gains for all",
"Issue - Defend the rights of LGBT individuals",
"Issue - Defend traditional marriage and religious beliefs",
"Issue - Expand domestic oil and gas production through drilling",
"Issue - Expand government and unemployment assistance for those in need",
"Issue - Prevent and prosecute abuse of government assistance programs",
"Issue - Protect a woman's right to choose",
"Issue - Protect gun owners' rights to defend themselves and others",
"Issue - Protect jobs and industry from unfair foreign trade",
"Issue - Protect the lives of the unborn",
"Issue - Provide a path to citizenship for undocumented immigrants",
"Issue - Reduce the size of military and number of military bases",
"Issue - Reform policing and stop racial profiling",
"Issue - Regulate CO2 emissions to combat global warming",
"Issue - Strengthen border security to stop illegal immigration",
"Issue - Strengthen gun control through commonsense restrictions",
"Issue - Strengthen our military and national defense",
"Issue - Toughen sentences and penalties for criminals")

core_for_ggplot <- data.frame(labels=labels,full_matrix_clean)
core_for_ggplot$iv_order <- factor(core_for_ggplot$labels, as.character(core_for_ggplot$labels))

omits=c("Gender - Male",
"Race - White",
"Religion - None",
"Occupation - Attorney",
"Personality - Decent",
"Endorsements - Major area newspapers",
"Record - Help my constituents get the benefits they deserve",
"Issue - Promote expanding free trade agreements")

label.groups=str_sub(labels,1,str_locate(labels,pattern='-')[,1]-2)
o.column=grep(names(core_for_ggplot),pattern='i_est')

#core_for_ggplot=lableOrder(xmat=core_for_ggplot,labels,label.groups,omits,o.column)
#write.csv(core_for_ggplot,"csv/choice_primary_core_for_ggplot.csv")

###########################################
###Then, plot those models

#Doing the within-factor sorting in shitty hack
#core_for_ggplot <- read.csv("csv/choice_primary_core_for_ggplot.csv")
o.order <- read.csv("data/core_for_ggplot_global.csv")$iv_order

core_for_ggplot=reOrder(x=core_for_ggplot,o=o.order)

# augment w/ scores bivariate coefficient || excluded ...

# ideology
primary_elec_dems_ideo <- lm(data=dems,dv_choice~
pty + scores,weights = 1/wt)
primary_elec_dems_ideo_clse <- clse.f(dems,primary_elec_dems_ideo,dems$respondent)

primary_elec_reps_ideo <- lm(data=reps,dv_choice~
pty + scores,weights = 1/wt)
primary_elec_reps_ideo_clse <- clse.f(reps,primary_elec_reps_ideo,reps$respondent)

primary_elec_inds_ideo <- lm(data=inds,dv_choice~
pty + scores,weights = 1/wt)
primary_elec_inds_ideo_clse <- clse.f(inds,primary_elec_inds_ideo,inds$respondent)

core_for_ggplot[,1]=as.character(core_for_ggplot[,1])
#core_for_ggplot[,2]=as.character(core_for_ggplot[,2])
core_for_ggplot[,8]=as.character(core_for_ggplot[,8])
#core_for_ggplot=rbind(core_for_ggplot,c('i_ideology','Overall Ideology Rating',
	#primary_elec_dems_ideo_clse[2,1],primary_elec_dems_ideo_clse[2,2],
	#primary_elec_inds_ideo_clse[2,1],primary_elec_inds_ideo_clse[2,2],
	#primary_elec_reps_ideo_clse[2,1],primary_elec_reps_ideo_clse[2,2],
	#'Overall Ideology Rating'))

attach(core_for_ggplot)
labels=core_for_ggplot$labels


ggplot_stacked <- data.frame(
labels=c(as.character(labels), as.character(labels), as.character(labels)),
PID=c(rep("Democrat",65),rep("Independent",65),rep("Republican",65)),
estimate=c(d_estimate,i_estimate,r_estimate),
se=c(d_se,i_se,r_se))

#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

ggplot_stacked=ggplot_stacked[!grepl(ggplot_stacked$PID,pattern='Independent'),]
#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

detach(core_for_ggplot)
attach(ggplot_stacked)


ggplot_stacked$estimate=as.numeric(as.character(ggplot_stacked$estimate))
ggplot_stacked$se=as.numeric(as.character(ggplot_stacked$se))

#exclude overall ideology
#ggplot_stacked=ggplot_stacked[-c(grep(as.character(ggplot_stacked[,1]),pattern='Overall')),]
ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
#write.csv(ggplot_stacked,"csv/choice_primary_ggplot_stacked.csv")
ggplot_stacked=ggplot_stacked[grep(ggplot_stacked[,1],pattern='Issue'),]

attach(ggplot_stacked)
labels=unique(ggplot_stacked$labels)

# choice_primary.R
primary_ggplot_stacked=ggplot_stacked

pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Democrat',replace=paste(sep='','Democrat (N=',length(unique(dems$respondent)),')'))
ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Republican',replace=paste(sep='','Republican (N=',length(unique(reps$respondent)),')'))



pdf(paste(sep='',"appendix/figures/agreement_simulation_wgt_",mq,".pdf"), width=8, height=5, pointsize=1)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.45,0.45) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(5.75,5.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=25.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=38.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()



i=i+1
mq=c('L','M','H')[i]
# low/medium/high
attach(reps)
primary_elec_reps <- lm(y_choice~
pty + g_female+
re_black+re_hispanic+
r_catholic+r_evangelical+r_protestant+
o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
rec_refuse+rec_secure+rec_stand+rec_work+
i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
weights=1/wt,subset=which(sophisticated==mq))

primary_elec_reps_clse <- clse.f(reps[which(sophisticated==mq),],primary_elec_reps,respondent[which(sophisticated==mq)])
detach(reps)

attach(dems)
primary_elec_dems <- lm(y_choice~
pty + g_female+
re_black+re_hispanic+
r_catholic+r_evangelical+r_protestant+
o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
rec_refuse+rec_secure+rec_stand+rec_work+
i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
weights=1/wt,subset=which(sophisticated==mq))

primary_elec_dems_clse <- clse.f(dems[which(sophisticated==mq),],primary_elec_dems,respondent[which(sophisticated==mq)])
detach(dems)

primary_elec_inds=primary_elec_dems
primary_elec_inds_clse =primary_elec_dems_clse


results_matrix <- cbind(primary_elec_dems$coefficients,primary_elec_inds$coefficients,primary_elec_reps$coefficients)
# remove pty coef
results_matrix=results_matrix[-c(2),]
results_matrix=cbind(results_matrix,NA,NA,NA)

for(j in 1:nrow(results_matrix)){
	ix=which(names(primary_elec_dems_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,4]=primary_elec_dems_clse[ix,2]
	}

	ix=which(names(primary_elec_inds_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,5]=primary_elec_inds_clse[ix,2]
	}

	ix=which(names(primary_elec_reps_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,6]=primary_elec_reps_clse[ix,2]
	}
}


results_matrix <- results_matrix[,c(1,4,2,5,3,6)]

colnames(results_matrix) <- c("d_estimate","d_se","i_estimate","i_se","r_estimate","r_se")
results_matrix <- results_matrix[-c(1),]
results_matrix_withnames <- cbind(var=rownames(results_matrix),results_matrix)
rownames(results_matrix_withnames) <- seq(1,57,by=1)

#results_matrix_withnames[is.na(results_matrix_withnames)]<- 0
##Now inserting rows for the omitted levels

#omitted: male, white, no religion, attorney, decent, newspaper endorsements, record = helping constituents, raising taxes for both issues

g_male <- c("g_male",rep(0,6))
re_white <- c("re_white",rep(0,6))
r_none <- c("r_none",rep(0,6))
o_attorney <- c("o_attorney",rep(0,6))
p_decent <- c("p_decent",rep(0,6))
e_newspapers <- c("e_newspapers",rep(0,6))
rec_help <- c("rec_help",rep(0,6))
#i_raisetaxes <- c("i1_raisetaxes",rep(0,6))
i_freetrade <- c("i1_freetrade",rep(0,6))

full_matrix <- rbind(
g_male,
results_matrix_withnames[1,],
re_white,
results_matrix_withnames[2:3,],
r_none,
results_matrix_withnames[4:6,],
o_attorney,
results_matrix_withnames[7:15,],
p_decent,
results_matrix_withnames[16:22,],
e_newspapers,
results_matrix_withnames[23:34,],
rec_help,
results_matrix_withnames[35:38,],
i_freetrade,
results_matrix_withnames[39:57,]
)

rownames(full_matrix) <- full_matrix[,1]
full_matrix <- full_matrix[,2:ncol(full_matrix)]
full_matrix_clean <- apply(full_matrix,2,as.numeric)
rownames(full_matrix_clean) <- rownames(full_matrix)

labels <- c(
"Gender - Male",
"Gender - Female",
"Race - White",
"Race - Black",
"Race - Hispanic",
"Religion - None",
"Religion - Catholic",
"Religion - Evangelical Protestant",
"Religion - Protestant",
"Occupation - Attorney",
"Occupation - CEO",
"Occupation - City Council Member",
"Occupation - Factory Foreman",
"Occupation - Farmer",
"Occupation - Former US Army Major",
"Occupation - Political Staffer",
"Occupation - Small Business Owner",
"Occupation - State Legislator",
"Occupation - Teacher",
"Personality - Decent",
"Personality - Compassionate",
"Personality - Empathetic",
"Personality - Inspiring",
"Personality - Intelligent",
"Personality - Knowledgeable",
"Personality - Moral",
"Personality - Strong Leader",
"Endorsements - Major area newspapers",
"Endorsements - Business groups",
"Endorsements - Christian groups",
"Endorsements - Civil rights groups",
"Endorsements - Energy groups",
"Endorsements - Environmental groups",
"Endorsements - Gun control groups",
"Endorsements - Gun rights groups",
"Endorsements - Labor unions",
"Endorsements - Reproductive rights groups",
"Endorsements - Tax reform groups",
"Endorsements - Tea Party groups",
"Endorsements - Veterans groups",
"Record - Help my constituents get the benefits they deserve",
"Record - Refuse to compromise my principles even when it means taking on my party",
"Record - Secure appointment to a powerful legislative committee",
"Record - Stand with my party to do what's right",
"Record - Work across the aisle to get things done",
"Issue - Promote expanding free trade agreements",
"Issue - Raise taxes on those making more than $250,000 a year",
"Issue - Cut taxes on income and capital gains for all",
"Issue - Defend the rights of LGBT individuals",
"Issue - Defend traditional marriage and religious beliefs",
"Issue - Expand domestic oil and gas production through drilling",
"Issue - Expand government and unemployment assistance for those in need",
"Issue - Prevent and prosecute abuse of government assistance programs",
"Issue - Protect a woman's right to choose",
"Issue - Protect gun owners' rights to defend themselves and others",
"Issue - Protect jobs and industry from unfair foreign trade",
"Issue - Protect the lives of the unborn",
"Issue - Provide a path to citizenship for undocumented immigrants",
"Issue - Reduce the size of military and number of military bases",
"Issue - Reform policing and stop racial profiling",
"Issue - Regulate CO2 emissions to combat global warming",
"Issue - Strengthen border security to stop illegal immigration",
"Issue - Strengthen gun control through commonsense restrictions",
"Issue - Strengthen our military and national defense",
"Issue - Toughen sentences and penalties for criminals")

core_for_ggplot <- data.frame(labels=labels,full_matrix_clean)
core_for_ggplot$iv_order <- factor(core_for_ggplot$labels, as.character(core_for_ggplot$labels))

omits=c("Gender - Male",
"Race - White",
"Religion - None",
"Occupation - Attorney",
"Personality - Decent",
"Endorsements - Major area newspapers",
"Record - Help my constituents get the benefits they deserve",
"Issue - Promote expanding free trade agreements")

label.groups=str_sub(labels,1,str_locate(labels,pattern='-')[,1]-2)
o.column=grep(names(core_for_ggplot),pattern='i_est')

#core_for_ggplot=lableOrder(xmat=core_for_ggplot,labels,label.groups,omits,o.column)
#write.csv(core_for_ggplot,"csv/choice_primary_core_for_ggplot.csv")

###########################################
###Then, plot those models

#Doing the within-factor sorting in shitty hack
#core_for_ggplot <- read.csv("csv/choice_primary_core_for_ggplot.csv")
o.order <- read.csv("data/core_for_ggplot_global.csv")$iv_order

core_for_ggplot=reOrder(x=core_for_ggplot,o=o.order)

# augment w/ scores bivariate coefficient || excluded ...

# ideology
primary_elec_dems_ideo <- lm(data=dems,dv_choice~
pty + scores,weights = 1/wt)
primary_elec_dems_ideo_clse <- clse.f(dems,primary_elec_dems_ideo,dems$respondent)

primary_elec_reps_ideo <- lm(data=reps,dv_choice~
pty + scores,weights = 1/wt)
primary_elec_reps_ideo_clse <- clse.f(reps,primary_elec_reps_ideo,reps$respondent)

primary_elec_inds_ideo <- lm(data=inds,dv_choice~
pty + scores,weights = 1/wt)
primary_elec_inds_ideo_clse <- clse.f(inds,primary_elec_inds_ideo,inds$respondent)

core_for_ggplot[,1]=as.character(core_for_ggplot[,1])
#core_for_ggplot[,2]=as.character(core_for_ggplot[,2])
core_for_ggplot[,8]=as.character(core_for_ggplot[,8])
#core_for_ggplot=rbind(core_for_ggplot,c('i_ideology','Overall Ideology Rating',
	#primary_elec_dems_ideo_clse[2,1],primary_elec_dems_ideo_clse[2,2],
	#primary_elec_inds_ideo_clse[2,1],primary_elec_inds_ideo_clse[2,2],
	#primary_elec_reps_ideo_clse[2,1],primary_elec_reps_ideo_clse[2,2],
	#'Overall Ideology Rating'))

attach(core_for_ggplot)
labels=core_for_ggplot$labels


ggplot_stacked <- data.frame(
labels=c(as.character(labels), as.character(labels), as.character(labels)),
PID=c(rep("Democrat",65),rep("Independent",65),rep("Republican",65)),
estimate=c(d_estimate,i_estimate,r_estimate),
se=c(d_se,i_se,r_se))

#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

ggplot_stacked=ggplot_stacked[!grepl(ggplot_stacked$PID,pattern='Independent'),]
#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

detach(core_for_ggplot)
attach(ggplot_stacked)


ggplot_stacked$estimate=as.numeric(as.character(ggplot_stacked$estimate))
ggplot_stacked$se=as.numeric(as.character(ggplot_stacked$se))

#exclude overall ideology
#ggplot_stacked=ggplot_stacked[-c(grep(as.character(ggplot_stacked[,1]),pattern='Overall')),]
ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
#write.csv(ggplot_stacked,"csv/choice_primary_ggplot_stacked.csv")
ggplot_stacked=ggplot_stacked[grep(ggplot_stacked[,1],pattern='Issue'),]

attach(ggplot_stacked)
labels=unique(ggplot_stacked$labels)

# choice_primary.R
primary_ggplot_stacked=ggplot_stacked

pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Democrat',replace=paste(sep='','Democrat (N=',length(unique(dems$respondent)),')'))
ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Republican',replace=paste(sep='','Republican (N=',length(unique(reps$respondent)),')'))



pdf(paste(sep='',"appendix/figures/agreement_simulation_wgt_",mq,".pdf"), width=8, height=5, pointsize=1)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.45,0.45) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(5.75,5.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=25.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=38.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()


i=i+1
mq=c('L','M','H')[i]
# low/medium/high
attach(reps)
primary_elec_reps <- lm(y_choice~
pty + g_female+
re_black+re_hispanic+
r_catholic+r_evangelical+r_protestant+
o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
rec_refuse+rec_secure+rec_stand+rec_work+
i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
weights=1/wt,subset=which(sophisticated==mq))

primary_elec_reps_clse <- clse.f(reps[which(sophisticated==mq),],primary_elec_reps,respondent[which(sophisticated==mq)])
detach(reps)

attach(dems)
primary_elec_dems <- lm(y_choice~
pty + g_female+
re_black+re_hispanic+
r_catholic+r_evangelical+r_protestant+
o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
rec_refuse+rec_secure+rec_stand+rec_work+
i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
weights=1/wt,subset=which(sophisticated==mq))

primary_elec_dems_clse <- clse.f(dems[which(sophisticated==mq),],primary_elec_dems,respondent[which(sophisticated==mq)])
detach(dems)

primary_elec_inds=primary_elec_dems
primary_elec_inds_clse =primary_elec_dems_clse


results_matrix <- cbind(primary_elec_dems$coefficients,primary_elec_inds$coefficients,primary_elec_reps$coefficients)
# remove pty coef
results_matrix=results_matrix[-c(2),]
results_matrix=cbind(results_matrix,NA,NA,NA)

for(j in 1:nrow(results_matrix)){
	ix=which(names(primary_elec_dems_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,4]=primary_elec_dems_clse[ix,2]
	}

	ix=which(names(primary_elec_inds_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,5]=primary_elec_inds_clse[ix,2]
	}

	ix=which(names(primary_elec_reps_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,6]=primary_elec_reps_clse[ix,2]
	}
}


results_matrix <- results_matrix[,c(1,4,2,5,3,6)]

colnames(results_matrix) <- c("d_estimate","d_se","i_estimate","i_se","r_estimate","r_se")
results_matrix <- results_matrix[-c(1),]
results_matrix_withnames <- cbind(var=rownames(results_matrix),results_matrix)
rownames(results_matrix_withnames) <- seq(1,57,by=1)

#results_matrix_withnames[is.na(results_matrix_withnames)]<- 0
##Now inserting rows for the omitted levels

#omitted: male, white, no religion, attorney, decent, newspaper endorsements, record = helping constituents, raising taxes for both issues

g_male <- c("g_male",rep(0,6))
re_white <- c("re_white",rep(0,6))
r_none <- c("r_none",rep(0,6))
o_attorney <- c("o_attorney",rep(0,6))
p_decent <- c("p_decent",rep(0,6))
e_newspapers <- c("e_newspapers",rep(0,6))
rec_help <- c("rec_help",rep(0,6))
#i_raisetaxes <- c("i1_raisetaxes",rep(0,6))
i_freetrade <- c("i1_freetrade",rep(0,6))

full_matrix <- rbind(
g_male,
results_matrix_withnames[1,],
re_white,
results_matrix_withnames[2:3,],
r_none,
results_matrix_withnames[4:6,],
o_attorney,
results_matrix_withnames[7:15,],
p_decent,
results_matrix_withnames[16:22,],
e_newspapers,
results_matrix_withnames[23:34,],
rec_help,
results_matrix_withnames[35:38,],
i_freetrade,
results_matrix_withnames[39:57,]
)

rownames(full_matrix) <- full_matrix[,1]
full_matrix <- full_matrix[,2:ncol(full_matrix)]
full_matrix_clean <- apply(full_matrix,2,as.numeric)
rownames(full_matrix_clean) <- rownames(full_matrix)

labels <- c(
"Gender - Male",
"Gender - Female",
"Race - White",
"Race - Black",
"Race - Hispanic",
"Religion - None",
"Religion - Catholic",
"Religion - Evangelical Protestant",
"Religion - Protestant",
"Occupation - Attorney",
"Occupation - CEO",
"Occupation - City Council Member",
"Occupation - Factory Foreman",
"Occupation - Farmer",
"Occupation - Former US Army Major",
"Occupation - Political Staffer",
"Occupation - Small Business Owner",
"Occupation - State Legislator",
"Occupation - Teacher",
"Personality - Decent",
"Personality - Compassionate",
"Personality - Empathetic",
"Personality - Inspiring",
"Personality - Intelligent",
"Personality - Knowledgeable",
"Personality - Moral",
"Personality - Strong Leader",
"Endorsements - Major area newspapers",
"Endorsements - Business groups",
"Endorsements - Christian groups",
"Endorsements - Civil rights groups",
"Endorsements - Energy groups",
"Endorsements - Environmental groups",
"Endorsements - Gun control groups",
"Endorsements - Gun rights groups",
"Endorsements - Labor unions",
"Endorsements - Reproductive rights groups",
"Endorsements - Tax reform groups",
"Endorsements - Tea Party groups",
"Endorsements - Veterans groups",
"Record - Help my constituents get the benefits they deserve",
"Record - Refuse to compromise my principles even when it means taking on my party",
"Record - Secure appointment to a powerful legislative committee",
"Record - Stand with my party to do what's right",
"Record - Work across the aisle to get things done",
"Issue - Promote expanding free trade agreements",
"Issue - Raise taxes on those making more than $250,000 a year",
"Issue - Cut taxes on income and capital gains for all",
"Issue - Defend the rights of LGBT individuals",
"Issue - Defend traditional marriage and religious beliefs",
"Issue - Expand domestic oil and gas production through drilling",
"Issue - Expand government and unemployment assistance for those in need",
"Issue - Prevent and prosecute abuse of government assistance programs",
"Issue - Protect a woman's right to choose",
"Issue - Protect gun owners' rights to defend themselves and others",
"Issue - Protect jobs and industry from unfair foreign trade",
"Issue - Protect the lives of the unborn",
"Issue - Provide a path to citizenship for undocumented immigrants",
"Issue - Reduce the size of military and number of military bases",
"Issue - Reform policing and stop racial profiling",
"Issue - Regulate CO2 emissions to combat global warming",
"Issue - Strengthen border security to stop illegal immigration",
"Issue - Strengthen gun control through commonsense restrictions",
"Issue - Strengthen our military and national defense",
"Issue - Toughen sentences and penalties for criminals")

core_for_ggplot <- data.frame(labels=labels,full_matrix_clean)
core_for_ggplot$iv_order <- factor(core_for_ggplot$labels, as.character(core_for_ggplot$labels))

omits=c("Gender - Male",
"Race - White",
"Religion - None",
"Occupation - Attorney",
"Personality - Decent",
"Endorsements - Major area newspapers",
"Record - Help my constituents get the benefits they deserve",
"Issue - Promote expanding free trade agreements")

label.groups=str_sub(labels,1,str_locate(labels,pattern='-')[,1]-2)
o.column=grep(names(core_for_ggplot),pattern='i_est')

#core_for_ggplot=lableOrder(xmat=core_for_ggplot,labels,label.groups,omits,o.column)
#write.csv(core_for_ggplot,"csv/choice_primary_core_for_ggplot.csv")

###########################################
###Then, plot those models

#Doing the within-factor sorting in shitty hack
#core_for_ggplot <- read.csv("csv/choice_primary_core_for_ggplot.csv")
o.order <- read.csv("data/core_for_ggplot_global.csv")$iv_order

core_for_ggplot=reOrder(x=core_for_ggplot,o=o.order)

# augment w/ scores bivariate coefficient || excluded ...

# ideology
primary_elec_dems_ideo <- lm(data=dems,dv_choice~
pty + scores,weights = 1/wt)
primary_elec_dems_ideo_clse <- clse.f(dems,primary_elec_dems_ideo,dems$respondent)

primary_elec_reps_ideo <- lm(data=reps,dv_choice~
pty + scores,weights = 1/wt)
primary_elec_reps_ideo_clse <- clse.f(reps,primary_elec_reps_ideo,reps$respondent)

primary_elec_inds_ideo <- lm(data=inds,dv_choice~
pty + scores,weights = 1/wt)
primary_elec_inds_ideo_clse <- clse.f(inds,primary_elec_inds_ideo,inds$respondent)

core_for_ggplot[,1]=as.character(core_for_ggplot[,1])
#core_for_ggplot[,2]=as.character(core_for_ggplot[,2])
core_for_ggplot[,8]=as.character(core_for_ggplot[,8])
#core_for_ggplot=rbind(core_for_ggplot,c('i_ideology','Overall Ideology Rating',
	#primary_elec_dems_ideo_clse[2,1],primary_elec_dems_ideo_clse[2,2],
	#primary_elec_inds_ideo_clse[2,1],primary_elec_inds_ideo_clse[2,2],
	#primary_elec_reps_ideo_clse[2,1],primary_elec_reps_ideo_clse[2,2],
	#'Overall Ideology Rating'))

attach(core_for_ggplot)
labels=core_for_ggplot$labels


ggplot_stacked <- data.frame(
labels=c(as.character(labels), as.character(labels), as.character(labels)),
PID=c(rep("Democrat",65),rep("Independent",65),rep("Republican",65)),
estimate=c(d_estimate,i_estimate,r_estimate),
se=c(d_se,i_se,r_se))

#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

ggplot_stacked=ggplot_stacked[!grepl(ggplot_stacked$PID,pattern='Independent'),]
#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

detach(core_for_ggplot)
attach(ggplot_stacked)


ggplot_stacked$estimate=as.numeric(as.character(ggplot_stacked$estimate))
ggplot_stacked$se=as.numeric(as.character(ggplot_stacked$se))

#exclude overall ideology
#ggplot_stacked=ggplot_stacked[-c(grep(as.character(ggplot_stacked[,1]),pattern='Overall')),]
ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
#write.csv(ggplot_stacked,"csv/choice_primary_ggplot_stacked.csv")
ggplot_stacked=ggplot_stacked[grep(ggplot_stacked[,1],pattern='Issue'),]

attach(ggplot_stacked)
labels=unique(ggplot_stacked$labels)

# choice_primary.R
primary_ggplot_stacked=ggplot_stacked

pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Democrat',replace=paste(sep='','Democrat (N=',length(unique(dems$respondent)),')'))
ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Republican',replace=paste(sep='','Republican (N=',length(unique(reps$respondent)),')'))



pdf(paste(sep='',"appendix/figures/agreement_simulation_wgt_",mq,".pdf"), width=8, height=5, pointsize=1)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.45,0.45) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(5.75,5.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=25.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=38.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()

#END
